/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.postorder.flow.audit;

import cn.hutool.core.lang.Assert;
import com.lxk.oms.order.domain.vo.OmsOrderItemVo;
import com.lxk.oms.order.service.IOmsOrderItemService;
import com.lxk.oms.postorder.domain.bo.OmsPostOrderAddressBo;
import com.lxk.oms.postorder.domain.enums.PostOrderEnums;
import com.lxk.oms.postorder.domain.req.AuditPostOrderReq;
import com.lxk.oms.postorder.domain.vo.OmsPostOrderItemVo;
import com.lxk.oms.postorder.domain.vo.OmsPostOrderVo;
import com.lxk.oms.postorder.service.IOmsPostOrderAddressService;
import com.lxk.oms.postorder.service.IOmsPostOrderItemService;
import com.lxk.oms.postorder.service.IOmsPostOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.ValidationUtils;

import java.util.List;

/**
 * 审核售后参数填充
 *
 * @author heyi
 * 2024/10/22
 */
@Slf4j
@AllArgsConstructor
@LiteflowComponent(value = AuditNodeConst.PO_FILL_NODE)
public class AuditFillNode extends NodeComponent {
    private final IOmsPostOrderService postOrderService;
    private final IOmsPostOrderItemService postOrderItemService;
    private final IOmsPostOrderAddressService postOrderAddressService;
    private final IOmsOrderItemService omsOrderItemService;

    @Override
    public void beforeProcess() {
        AuditPostOrderReq req = this.getRequestData();
        log.info("售后单审核参数{}", req);
        PoAuditContext contextBean = this.getContextBean(PoAuditContext.class);
        ValidationUtils.validate(req);
        if (req.getPostOrderStatus()) {
            AuditPostOrderReq.Address merAddress = req.getMerAddress();
            Assert.notNull(merAddress);
        }
    }

    @Override
    public void process() throws Exception {
        AuditPostOrderReq req = this.getRequestData();
        PoAuditContext contextBean = this.getContextBean(PoAuditContext.class);
        MapstructUtils.convert(req, contextBean);
        String postOrderNo = contextBean.getPostOrderNo();
        OmsPostOrderVo omsPostOrderVo = postOrderService.queryByNo(postOrderNo, PostOrderEnums.PostOrderType.REFUND_RETURN.getPostOrderType());
        contextBean.setPostOrderVo(omsPostOrderVo);
        ValidationUtils.validate(contextBean);
        Boolean postOrderStatus = contextBean.getPostOrderStatus();
        if (postOrderStatus) {
            // 设置商家收货地址
            Long orderId = omsPostOrderVo.getOrderId();
            List<OmsOrderItemVo> omsOrderItemVos = omsOrderItemService.queryByOrderIdList(orderId);
            contextBean.setOmsOrderItemVos(omsOrderItemVos);
            // 设置售后子订单
            List<OmsPostOrderItemVo> omsPostOrderItemVos = postOrderItemService.queryPostOrderId(omsPostOrderVo.getId());
            contextBean.setPostOrderItemVoList(omsPostOrderItemVos);
            // 设置商家收货地址
            AuditPostOrderReq.Address merAddress = req.getMerAddress();
            OmsPostOrderAddressBo add = MapstructUtils.convert(merAddress, OmsPostOrderAddressBo.class);
            contextBean.setOrderAddressBo(add);
        }

    }

}
